package com.atguigu.mall.mallproduct.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.mall.mallproduct.entity.PmsCategoryEntity;
import com.atguigu.mall.mallproduct.service.PmsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.mall.mallproduct.entity.PmsAttrGroupEntity;
import com.atguigu.mall.mallproduct.service.PmsAttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

/**
 * 属性分组
 *
 * @author Fuc
 * @email leftbear@gmail.com
 * @date 2020-10-21 14:01:50
 */
@RestController
@RequestMapping("mallproduct/pmsattrgroup")
public class PmsAttrGroupController {
  @Autowired private PmsAttrGroupService pmsAttrGroupService;

  @Autowired
  private PmsCategoryService pmsCategoryService;

  /** 列表 */
  @RequestMapping("/list")
  public R list(
      @RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
//    PageUtils page = pmsAttrGroupService.queryPage(params);

    PageUtils page = pmsAttrGroupService.queryPage(params,catelogId);
    return R.ok().put("page", page);
  }

  /** 信息 */
  @RequestMapping("/info/{attrGroupId}")
  public R info(@PathVariable("attrGroupId") Long attrGroupId) {
    PmsAttrGroupEntity pmsAttrGroup = pmsAttrGroupService.getById(attrGroupId);
    Long catelogId = pmsAttrGroup.getCatelogId();
    Long[] path =  pmsCategoryService.findCatelogPath(catelogId);
    return R.ok().put("pmsAttrGroup", pmsAttrGroup);
  }

  /** 保存 */
  @RequestMapping("/save")
  public R save(@RequestBody PmsAttrGroupEntity pmsAttrGroup) {
    pmsAttrGroupService.save(pmsAttrGroup);

    return R.ok();
  }

  /** 修改 */
  @RequestMapping("/update")
  public R update(@RequestBody PmsAttrGroupEntity pmsAttrGroup) {
    pmsAttrGroupService.updateById(pmsAttrGroup);

    return R.ok();
  }

  /** 删除 */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] attrGroupIds) {
    pmsAttrGroupService.removeByIds(Arrays.asList(attrGroupIds));

    return R.ok();
  }
}
